package com.earnings.okhttputils.utils.ui.activity.user;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.dialog.ShareDialog;
import com.earnings.okhttputils.utils.utils.CommonUtil;
import com.earnings.okhttputils.utils.utils.GlideRoundTransform;
import com.earnings.okhttputils.utils.utils.ZxingUtil;
import com.google.zxing.WriterException;

import java.util.Map;



/*
我的二维码
 */

public class UserMyCodeActivity extends GodLeftHandBaseActivity implements View.OnClickListener {

    private ImageView codeImageView, head;
    private String url;

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_user_mycode;
    }

    @Override
    protected void init() {

        setTitle( "我的二维码" );
        findViewById( R.id.sumbit ).setOnClickListener( this );
        codeImageView = (ImageView) findViewById( R.id.code );
        head = (ImageView) findViewById( R.id.head );
        setText( R.id.name, CommonUtil.getUser().getNickname() );
        setText( R.id.info, CommonUtil.getUser().getEmail() );
        setGlideImagView( R.id.head, CommonUtil.getUser().getHead_pic(), R.mipmap.head_icon );

        loadMyCode();
    }


    @Override
    public void onClick(View v) {

        //    super.onClick(v);


        int i = v.getId();
        if (i == R.id.top_right_tv) {
            skipActivity( UserRecommendedFriendsActivity.class );

        } else if (i == R.id.sumbit) {
            ShareDialog share = new ShareDialog( getContext() );
            share.setCont( "招财汇宝轻松赚钱,持续收益！" );
            share.setUrl( url );
            share.show();

        }
    }

    public void initCode(final String url) {
        if(CommonUtil.getUser().getHead_pic().equals( HttpUrl.index )){
            Bitmap code = null;
            try {
                code = ZxingUtil.createQRCode( url, 1000 );
            } catch (WriterException e) {
                e.printStackTrace();
            }
            codeImageView.setImageBitmap( code );
        }else {
            Glide.with( this ).load( CommonUtil.getUser().getHead_pic() )
                    .asBitmap()
                    .fitCenter().
                    transform( new GlideRoundTransform( this, 20 ) )
                    .into( new SimpleTarget( 200, 200 ) {

                        @Override
                        public void onResourceReady(Object resource, GlideAnimation glideAnimation) {

                            Bitmap code = ZxingUtil.createQRImage( url, 1000, (Bitmap) resource );
                            codeImageView.setImageBitmap( code );
                        }


                    } );
        }
    }


    public void loadMyCode() {
        showProgress();
        Map map = new HttpMap( getContext() );


        OkHttpUtils.post().url( HttpUrl.USER_MYCODE_URL ).params( map ).build().execute( new HttpObjectCallback<String>( getContext() ) {
            @Override
            public void onSuccess(String object, String msg) {
                url=object;
                initCode( object );
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {

                ToastUtils.showToast( getContext(), msg );
                dismissProgress();
            }


        } );


    }
}
